// TODO: Update to match your plugin's package name.
package com.xmgm.haoyou.android

import android.content.Context
import android.content.res.Configuration
import android.util.Log
import com.m3839.sdk.single.UnionFcmParam
import com.m3839.sdk.single.UnionFcmSDK
import com.m3839.sdk.single.UnionFcmUser
import com.m3839.sdk.single.UnionV2FcmListener
import org.godotengine.godot.Godot
import org.godotengine.godot.plugin.GodotPlugin
import org.godotengine.godot.plugin.SignalInfo
import org.godotengine.godot.plugin.UsedByGodot


class GodotAndroidPlugin(godot: Godot): GodotPlugin(godot) {

    override fun getPluginName() = "godot_haoyou"
    var context: Context? = null
    @UsedByGodot
    fun init_sdk(gameId: String) {
        Log.v(pluginName, "尝试实例化了")
        activity?.let { act ->
            val param = UnionFcmParam.Builder()
                .setGameId(gameId)
                .setOrientation(Configuration.ORIENTATION_LANDSCAPE)
                .build()
            UnionFcmSDK.init(activity, param, object : UnionV2FcmListener {
                override fun onSucceed(user: UnionFcmUser) {
                    if (user != null) {
                        //登录成功
                        Log.v(pluginName,user.toString())
                        Log.v(pluginName,"上面是测试信息")
                    }
                }

                override fun onFailed(code: Int, message: String) {
                    Log.v(pluginName,"code = $code,message = $message")
                }
            })
        } ?: Log.e(pluginName, "Activity is null, cannot initialize SDK.")
    }
    @UsedByGodot
    fun ShiFang(){
        context?.let {
            UnionFcmSDK.releaseSDK()
            as_to_godot_string("10001", "0")
        } ?: Log.w(pluginName, "Context is null, cannot release SDK.")
    }
    public fun as_to_godot_string(s:String,s2:String){
        emitSignal(tikTokSignalInfo.name,s,s2)

    }
    //    String.javaClass
    private val tikTokSignalInfo = SignalInfo("as_to_gd",String::class.java,String::class.java)
    override fun getPluginSignals(): Set<SignalInfo> {
        val tag = this.javaClass.toString()

        Log.i(tag, "Registering $tikTokSignalInfo")
        return setOf(tikTokSignalInfo)
    }

}
